import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

import 'logic.dart';

class AgentLoginPage extends StatefulWidget {
  const AgentLoginPage({Key? key}) : super(key: key);

  @override
  State<AgentLoginPage> createState() => _AgentLoginPageState();
}

class _AgentLoginPageState extends State<AgentLoginPage> {
  final AgentLoginLogic logic = Get.put(AgentLoginLogic());

  // 平台列表数据
  final List<Map<String, dynamic>> platforms = [
    {
      'name': 'iTaoBuy',
      'icon': Icons.shopping_cart_rounded,
      'color': Color(0xFFFF6A00),
      'enabled': true,
      'description': 'Professional shopping agent platform',
    },
    {
      'name': 'Cnfans',
      'icon': Icons.favorite_rounded,
      'color': Color(0xFFE91E63),
      'enabled': false,
      'description': 'Coming soon',
    },
    {
      'name': 'AcBuy',
      'icon': Icons.store_rounded,
      'color': Color(0xFF2196F3),
      'enabled': false,
      'description': 'Coming soon',
    },
    {
      'name': 'OopBuy',
      'icon': Icons.local_shipping_rounded,
      'color': Color(0xFF4CAF50),
      'enabled': false,
      'description': 'Coming soon',
    },
    {
      'name': 'HooBuy',
      'icon': Icons.rocket_launch_rounded,
      'color': Color(0xFF9C27B0),
      'enabled': false,
      'description': 'Coming soon',
    },
  ];

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AgentLoginLogic>(builder: (get) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios_new_rounded, color: Colors.white, size: 20.r),
            onPressed: () => Navigator.of(context).pop(),
          ),
          centerTitle: true,
          title: Text(
            'Agent Login',
            style: StyleUtils.ts_1f_20_600.copyWith(color: Colors.white),
          ),
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                ColorUtils.themeColor,
                ColorUtils.themeColor.withValues(alpha: 0.8),
                ColorUtils.c_f8f8f8,
              ],
              stops: [0.0, 0.3, 0.8],
            ),
          ),
          child: SingleChildScrollView(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(height: 120.r),

                // 顶部装饰图标
                Container(
                  width: 80.r,
                  height: 80.r,
                  decoration: BoxDecoration(
                    color: Colors.white.withValues(alpha: 0.2),
                    borderRadius: BorderRadius.circular(40.r),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.white.withValues(alpha: 0.3),
                        blurRadius: 20.r,
                        offset: Offset(0, 8.r),
                      ),
                    ],
                  ),
                  child: Icon(
                    Icons.person_add_rounded,
                    color: Colors.white,
                    size: 40.r,
                  ),
                ),

                SizedBox(height: 24.r),

                // 标题
                Text(
                  'Agent Platforms',
                  style: StyleUtils.ts_ff_24_600.copyWith(
                    fontSize: 28.r,
                    fontWeight: FontWeight.bold,
                  ),
                ),

                SizedBox(height: 8.r),

                // 副标题
                Text(
                  'Choose your preferred shopping agent platform',
                  style: StyleUtils.ts_ff_14_400.copyWith(
                    color: Colors.white.withValues(alpha: 0.9),
                  ),
                  textAlign: TextAlign.center,
                ),

                SizedBox(height: 32.r),

                // 平台列表卡片
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 20.r),
                  padding: EdgeInsets.all(24.r),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20.r),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withValues(alpha: 0.08),
                        blurRadius: 20.r,
                        offset: Offset(0, 8.r),
                      ),
                    ],
                  ),
                  child: Column(
                    children: [
                      // 平台列表
                      ...platforms.map((platform) => _buildPlatformItem(platform)).toList(),
                    ],
                  ),
                ),

                SizedBox(height: 32.r),

                // 底部提示信息
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 20.r),
                  padding: EdgeInsets.all(16.r),
                  decoration: BoxDecoration(
                    color: Colors.white.withValues(alpha: 0.9),
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: Row(
                    children: [
                      Icon(
                        Icons.info_outline_rounded,
                        color: ColorUtils.themeColor,
                        size: 20.r,
                      ),
                      SizedBox(width: 12.r),
                      Expanded(
                        child: Text(
                          'Connect with professional shopping agents to enhance your shopping experience.',
                          style: StyleUtils.ts_99_12_400,
                        ),
                      ),
                    ],
                  ),
                ),

                SizedBox(height: 40.r),
              ],
            ),
          ),
        ),
      );
    });
  }

  Widget _buildPlatformItem(Map<String, dynamic> platform) {
    bool isEnabled = platform['enabled'] as bool;
    Color platformColor = platform['color'] as Color;
    IconData platformIcon = platform['icon'] as IconData;
    String platformName = platform['name'] as String;
    String description = platform['description'] as String;

    return Container(
      margin: EdgeInsets.only(bottom: 16.r),
      decoration: BoxDecoration(
        color: isEnabled ? Colors.white : ColorUtils.c_f8f8f8,
        borderRadius: BorderRadius.circular(16.r),
        border: Border.all(
          color: isEnabled ? platformColor.withValues(alpha: 0.2) : ColorUtils.c_dcdcdc,
          width: 1.r,
        ),
        boxShadow: isEnabled
            ? [
                BoxShadow(
                  color: platformColor.withValues(alpha: 0.1),
                  blurRadius: 8.r,
                  offset: Offset(0, 2.r),
                ),
              ]
            : null,
      ),
      child: Material(
        color: Colors.transparent,
        child: InkWell(
          borderRadius: BorderRadius.circular(16.r),
          onTap: isEnabled
              ? () {
                  // 处理iTaoBuy点击事件
                  _handlePlatformClick(platformName);
                }
              : null,
          child: Padding(
            padding: EdgeInsets.all(16.r),
            child: Row(
              children: [
                // 平台图标
                Container(
                  width: 48.r,
                  height: 48.r,
                  decoration: BoxDecoration(
                    color: isEnabled ? platformColor.withValues(alpha: 0.1) : ColorUtils.c_dcdcdc,
                    borderRadius: BorderRadius.circular(12.r),
                  ),
                  child: Icon(
                    platformIcon,
                    color: isEnabled ? platformColor : ColorUtils.c_999999,
                    size: 24.r,
                  ),
                ),

                SizedBox(width: 16.r),

                // 平台信息
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Text(
                            platformName,
                            style: isEnabled ? StyleUtils.ts_1f_16_600.copyWith(color: platformColor) : StyleUtils.ts_99_16_500,
                          ),
                        ],
                      ),
                      SizedBox(height: 4.r),
                      Text(
                        description,
                        style: isEnabled ? StyleUtils.ts_99_12_400 : StyleUtils.ts_99_12_400,
                      ),
                    ],
                  ),
                ),

                // 箭头图标
                Icon(
                  isEnabled ? Icons.arrow_forward_ios_rounded : Icons.lock_outline_rounded,
                  color: isEnabled ? platformColor : ColorUtils.c_999999,
                  size: 16.r,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _handlePlatformClick(String platformName) {
    // 处理平台点击事件
    if (platformName == 'iTaoBuy') {
      // 这里可以添加iTaoBuy的具体登录逻辑
      Get.snackbar(
        'iTaoBuy',
        'Connecting to iTaoBuy platform...',
        backgroundColor: ColorUtils.themeColor,
        colorText: Colors.white,
        snackPosition: SnackPosition.TOP,
      );
    }
  }

  @override
  void dispose() {
    Get.delete<AgentLoginLogic>();
    super.dispose();
  }
}
